package com.data.boot.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.alibaba.fastjson.JSON;
import com.data.boot.model.Field;
import com.data.boot.model.Record;
import com.data.boot.service.ScriptService;
import groovy.lang.Script;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by dwk on 2019/5/31.
 */
@RestController
public class TestController {
    private static final Log log = LogFactory.get();
    @Autowired
    private ScriptService scriptService;

    @RequestMapping("/test")
    public Object test(String param) throws SQLException {
        Script script = scriptService.getScript("test.groovy");
        Object text = script.invokeMethod("test", DateUtil.yesterday());
        System.out.println(JSON.toJSONString(text));
        return text;
    }

    @RequestMapping("/read")
    public String readScript(String name) throws FileNotFoundException {
        FileReader reader = new FileReader("D:/IdeaProjects/demo/src/main/resources/script/" + name);
        return reader.readString();
    }

    /**
     * 测试数据批量导入
     * @return
     * @throws IOException
     */
    @RequestMapping("load")
    public String load() throws IOException {
        Record record = new Record();
        List<Field> fields = new ArrayList<>();
        fields.add(new Field("\\", false));
        fields.add(new Field("\\", true));
        fields.add(new Field(" ", false));
        fields.add(new Field(" ", false));
        fields.add(new Field(" ", false));
        fields.add(new Field(" [", false));
        fields.add(new Field("]  ", true));
        fields.add(new Field("\" ", true));
        fields.add(new Field("  ", false));
        fields.add(new Field("  \\", false));
        fields.add(new Field("\\ \\", true));
        fields.add(new Field("\\ ", true));
        fields.add(new Field(" ", false));
        fields.add(new Field("\n", true));
        record.setFields(fields);
        String sql = "INSERT  INTO  od_infoplus_access_log VALUES (?,?,?,?,?,?)";
        String path = "C:\\Users\\Administrator\\Desktop\\localhost_access_log.2019-05-28.txt";
        Script script = scriptService.getScript("MysqlLoad.groovy");
        long startTime = System.currentTimeMillis(); //获取开始时间
        script.invokeMethod("load", new Object[]{sql, path, record});
//        script.evaluate(new File("D:\\IdeaProjects\\data-boot\\src\\main\\resources\\script\\groovy\\MysqlLoad.groovy"));
//        MySqlLoadDataService sqlLoadDataService=new MySqlLoadDataService();
//        sqlLoadDataService.load(sql,path,record);
        long endTime = System.currentTimeMillis();//获取结束时间
        log.info("程序运行时间：" + (endTime - startTime) / 1000 + "s"); //输出程序运行时间
        return "成功!";
    }
}
